var db;
var dbCreated = false;


db = window.openDatabase("DiarioDB", "1.0", "PhoneGap Diaro", 200000);

/*
function onDeviceReady() {
	alert("ready");
	console.log("opening database");
    db = window.openDatabase("DiarioDB", "1.0", "PhoneGap Demo", 200000);
	console.log("database opened");
    db.transaction(getmaterias, transaction_error);
}

*/
if (dbCreated)
	{
	
    	db.transaction(getmaterias, transaction_error);
	}else{
		
    	db.transaction(populateDB, transaction_error, populateDB_success);
	}
function transaction_error(tx, error) {
	$('#busy').hide();
    alert("Database Error: " + error);
}

function populateDB_success() {
	dbCreated = true;
    db.transaction(getmaterias, transaction_error);
}

/*
 * Lista materia
 */
function getmaterias(tx) {

	var sql = "select e.id, e.descrizione, count(e.id) reportCount " + 
				"from materia e  " +
				"group by e.id order by e.descrizione";
	tx.executeSql(sql, [], getmaterias_success);
	
	
}

/*
 * Popolo la pagina html  materiadetails
 * con la lista delle materie
 */
function getmaterias_success(tx, results) {
	
	$('#content').append('<div><ul data-role="listview"  id="materiaList"  data-theme="c"></ul></div>');
    var len = results.rows.length;
    for (var i=0; i<len; i++) {
    	var materia = results.rows.item(i);
		$('#materiaList').append('<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-first-child ui-btn-up-c"><div class="ui-btn-inner ui-li"><div class="ui-btn-text"><a href="#" class="ui-link-inherit">'+ materia.descrizione+'</a></div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span></div></li>');
    }
    $('#busy').hide();
	db = null;
}

/*
 * In caso non esiste creo la tabella Materia
 */
function populateDB(tx) {

	$('#busy').show();

	var sql = 
		"CREATE TABLE  IF NOT EXISTS  materia ( "+
		"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
		"descrizione VARCHAR(100))";
    	tx.executeSql(sql);

   }


/****
 *  
 */
var isOpenDivInserimento = false;
$("#idAddMateria").click(function(){

	if (!isOpenDivInserimento)
	{
    $("#idAddMateria").animate({
    	top:"236px",
      }, 1500 ); 
    $("#IdDivInseriemntoMateria").animate({
    	top:"-21px",
      }, 1500 ); 
    $("#aa").text('-');
	}
	else
		{
		$("#idAddMateria").animate({
			opacity: 1,
	    	top:"41px",
	      }, 1500 ); 
	    $("#IdDivInseriemntoMateria").animate({
	    	top:"-217px",
	      }, 1500 );
	    $("#aa").text('+');
		}
	isOpenDivInserimento =!isOpenDivInserimento;
 });


$("#idSalvaMateria").click(function(){
	db = window.openDatabase("DiarioDB", "1.0", "PhoneGap Diaro", 200000);
	db.transaction(insertMateria,transaction_error, insert_success);

	
	
});

function insertMateria(txt) {
	txt.executeSql("INSERT INTO materia (descrizione) VALUES ('"+ $("#idMateria").val()  +  "')");
	 
}

function insert_success(tx, results){
	
	$('#materiaList').append('<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-first-child ui-btn-up-c"><div class="ui-btn-inner ui-li"><div class="ui-btn-text"><a href="acura.html" class="ui-link-inherit">'+  $("#idMateria").val() +'</a></div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span></div></li>');

	
}